package com.mw.mwreaderdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


public class AT24C extends Activity {

	private Button btnReadData;        //读数据按钮
	private Button btnWriteData;       //写数据按钮

	private Spinner spPwdType;         //选择卡类型
	private Spinner spApplication;     //选择指定页

	private EditText etData;
	
	private EditText etOffset;         //偏移地址	
	private EditText etResult;         //显示列表信息处理

	//  上一次点击的时间 long型  
	private long beforeClick = 0;
	private String TAG = "AT24C系列";
	private int pwdType = 0;
	private int cardType = 0;          //卡片类型
	private int offset=0;              //初始化偏移值0
	private int length=0;              //初始化长度值0

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_at24c);

		btnReadData = (Button) findViewById(R.id.btnReadData);
		btnWriteData = (Button) findViewById(R.id.btnWriteData);

		spPwdType = (Spinner) findViewById(R.id.spPwdType);
		spApplication = (Spinner) findViewById(R.id.spApplication);


		etData = (EditText) findViewById(R.id.etData);
		etOffset = (EditText) findViewById(R.id.etOffset);		
		etResult = (EditText) findViewById(R.id.etResult);

		// 设置输入字符转换为大写
		etData.setTransformationMethod(new InputLowerToUpper());

		// 设置双击操作信息提示框清除数据
		etResult.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - beforeClick > 500) {
						beforeClick = System.currentTimeMillis();
					} else {
						etResult.setText("");
					}
				}
				return false;
			}
		});

		spPwdType.setOnItemSelectedListener(new OnItemSelectedListener() {              //读取卡类型 返回值
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
				// TODO Auto-generated method stub
				if(position==0) {
					cardType = 1;       //选择卡类型   赋值
				}else if(position==1) {
					cardType = 2;
				}else if(position==2) {
					cardType = 3;
				}else if(position==3) {
					cardType = 4;
				}else if(position==4) {
					cardType = 5;
				}else if(position==5) {
					cardType = 11;
				}else if(position==6) {
					cardType = 6;
				}else if(position==7) {
					cardType = 7;
				}else if(position==8) {
					cardType = 8;
				}
				offset=0;   length=128;//查询数据   偏移值  长度
				Log.i("tcp", "cardType"+cardType);	
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});

		btnReadData.setOnClickListener(new OnClickListener() {                             //读取数据  
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT24C.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}			
				//int cardType = 5 ;//5 -- AT24C16 卡
				try {
					etResultAddStr(MainActivity.reader.read24CXX(cardType,offset, length));//类型  偏移地址   数据
					Log.i("tcp", "读卡类型cardType"+cardType);	
					Log.i("tcp", "偏移offset"+offset);	
					Log.i("tcp", "读取数据length"+length);	
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		
		});

		btnWriteData.setOnClickListener(new OnClickListener() {                            //写数据   
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT24C.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				//int cardType = 5;//5 -- AT24C16 卡
				int offset   = Integer.parseInt(etOffset.getText().toString());//写入偏移地址
				String str   = etData.getText().toString();                    //写入字符串
				try {
					MainActivity.reader.write24CXX(cardType,offset, str);      //类型  偏移地址   数据
					etResultAddStr("写数据成功!");
					Log.i("tcp", "写入卡cardType"+cardType);
					Log.i("tcp", "偏移地址offset"+offset);
					Log.i("tcp", "写入数据str"+str);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
		
		spApplication.setOnItemSelectedListener(new OnItemSelectedListener() {           //选择翻页显示数据
			//读取卡类型 返回值******
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position1, long arg3) {
				// TODO Auto-generated method stub
				if(position1==0){//选择页面
					 offset=0;   length=256;   //查询数据   偏移值  长度
				}else if(position1==1){
					offset=256; length=256;
				}else if(position1==2){
					offset=512; length=256;
				}else if(position1==3){
					offset=768; length=256;
				}else if(position1==4){
					offset=1024; length=256;
				}else if(position1==5){
					offset=1280; length=256;
				}else if(position1==6){
					offset=1536; length=256;
				}else if(position1==7){
					offset=1972; length=256;
				}
				Log.i("tcp", "cardType"+cardType);	
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * @Description 提示信息框添加信息
	 * @param str
	 *            要添加的信息
	 * @return void
	 */
	private void etResultAddStr(String str) {
		etResult.setText(etResult.getText() + " " + str + "\n");
		etResult.setSelection(etResult.length());// 调整光标到最后一行
		Log.i("tcp", "str"+str);	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.at24c, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
